package com.sita.tingterest.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sita.bike.R;
import com.sita.tingterest.pojo.Evalue;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;


/**
 * Created by zhangde001 on 2015/12/9.
 */
public class EvalueListAdapter extends BaseAdapter {
    public Activity activity;
    public Context context;
    public ArrayList<Evalue> list;

    public EvalueListAdapter() {
    }

    public EvalueListAdapter(Context context, ArrayList<Evalue> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        //LayoutInflater inflator = activity.getLayoutInflater();
        if (convertView == null) {
            view = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.evalue_list_item, null);

            view.txtMessage = (TextView) convertView.findViewById(R.id.tv_message);
            view.txtNickName = (TextView) convertView.findViewById(R.id.tv_nickname);
            view.img = (ImageView) convertView.findViewById(R.id.head_imageView);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
        view.txtNickName.setText(list.get(position).getNickName());
        view.txtMessage.setText(list.get(position).getMessage());
        ImageLoader.getInstance().displayImage(list.get(position).getAvatar(), view.img);
        return convertView;
    }

    public static class ViewHolder {
        public ImageView img;
        public TextView txtMessage, txtNickName;
        public ViewHolder view;
        public View convertView;
    }

    private class GetImage extends AsyncTask<String, String, Bitmap> {
        private byte[] iconData;
        private ViewHolder view;
        private View converView;
        private Bitmap bmp;

        public GetImage(ViewHolder view, View convertView)

        {
            Log.i("MY TAG", "GetImage in AsynTask");
            this.view = view;
            this.converView = convertView;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                InputStream in = new URL(params[0]).openStream();
                Log.i("MY TAG", "params[0]" + params[0]);
                bmp = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.i("MYTAG", "tag" + e);
            }
            return bmp;

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {

            view.img.setImageBitmap(bitmap);
            super.onPostExecute(bitmap);


        }
    }

}
